<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>ActiveRecord::AttributeMethods::Read</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" href="../../../css/reset.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../../css/main.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../../css/github.css" type="text/css" media="screen" />
<script src="../../../js/jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../js/jquery-effect.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../js/main.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../js/highlight.pack.js" type="text/javascript" charset="utf-8"></script>

</head>

<body>     
    <div class="banner">
        
            <span>Ruby on Rails v4.0.0</span><br />
        
        <h1>
            <span class="type">Module</span> 
            ActiveRecord::AttributeMethods::Read 
            
        </h1>
        <ul class="files">
            
            <li><a href="../../../files/activerecord/lib/active_record/attribute_methods/read_rb.html">activerecord/lib/active_record/attribute_methods/read.rb</a></li>
            
        </ul>
    </div>
    <div id="bodyContent">
        <div id="content">
  


  


  
  


  
    <!-- Namespace -->
    <div class="sectiontitle">Namespace</div>
    <ul>
      
        <li>
          <span class="type">MODULE</span>
          <a href="Read/ClassMethods.html">ActiveRecord::AttributeMethods::Read::ClassMethods</a>
        </li>
      
    </ul>
  


  
    <!-- Method ref -->
    <div class="sectiontitle">Methods</div>
    <dl class="methods">
      
        <dt>R</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="Read.html#method-i-read_attribute">read_attribute</a>
              </li>
            
          </ul>
        </dd>
      
    </dl>
  

  



  

    

    

    
      <!-- Section constants -->
      <div class="sectiontitle">Constants</div>
      <table border='0' cellpadding='5'>
        
          <tr valign='top'>
            <td class="attr-name">ATTRIBUTE_TYPES_CACHED_BY_DEFAULT</td>
            <td>=</td>
            <td class="attr-value">[:datetime, :timestamp, :time, :date]</td>
          </tr>
          
            <tr valign='top'>
              <td>&nbsp;</td>
              <td colspan="2" class="attr-desc"></td>
            </tr>
          
        
      </table>
    


    


    <!-- Methods -->
        
      <div class="sectiontitle">Instance Public methods</div>
      
        <div class="method">
          <div class="title method-title" id="method-i-read_attribute">
            
              <b>read_attribute</b>(attr_name)
            
            <a href="Read.html#method-i-read_attribute" name="method-i-read_attribute" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Returns the value of the attribute identified by <code>attr_name</code>
after it has been typecast (for example, “2004-12-12” in a data column is
cast to a date object, like Date.new(2004, 12, 12)).</p>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-read_attribute_source')" id="l_method-i-read_attribute_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/57b0ae8011ab0502253631bd6fdbc0fc838b593e/activerecord/lib/active_record/attribute_methods/read.rb#L75" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-read_attribute_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activerecord/lib/active_record/attribute_methods/read.rb, line 75</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">read_attribute</span>(<span class="ruby-identifier">attr_name</span>)
  <span class="ruby-comment"># If it's cached, just return it</span>
  <span class="ruby-comment"># We use #[] first as a perf optimization for non-nil values. See https://gist.github.com/jonleighton/3552829.</span>
  <span class="ruby-identifier">name</span> = <span class="ruby-identifier">attr_name</span>.<span class="ruby-identifier">to_s</span>
  <span class="ruby-ivar">@attributes_cache</span>[<span class="ruby-identifier">name</span>] <span class="ruby-operator">||</span> <span class="ruby-ivar">@attributes_cache</span>.<span class="ruby-identifier">fetch</span>(<span class="ruby-identifier">name</span>) {
    <span class="ruby-identifier">column</span> = <span class="ruby-ivar">@columns_hash</span>.<span class="ruby-identifier">fetch</span>(<span class="ruby-identifier">name</span>) {
      <span class="ruby-keyword">return</span> <span class="ruby-ivar">@attributes</span>.<span class="ruby-identifier">fetch</span>(<span class="ruby-identifier">name</span>) {
        <span class="ruby-keyword">if</span> <span class="ruby-identifier">name</span> <span class="ruby-operator">==</span> <span class="ruby-string">'id'</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">primary_key</span> <span class="ruby-operator">!=</span> <span class="ruby-identifier">name</span>
          <span class="ruby-identifier">read_attribute</span>(<span class="ruby-keyword">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">primary_key</span>)
        <span class="ruby-keyword">end</span>
      }
    }

    <span class="ruby-identifier">value</span> = <span class="ruby-ivar">@attributes</span>.<span class="ruby-identifier">fetch</span>(<span class="ruby-identifier">name</span>) {
      <span class="ruby-keyword">return</span> <span class="ruby-identifier">block_given?</span> <span class="ruby-operator">?</span> <span class="ruby-keyword">yield</span>(<span class="ruby-identifier">name</span>) <span class="ruby-operator">:</span> <span class="ruby-keyword">nil</span>
    }

    <span class="ruby-keyword">if</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">cache_attribute?</span>(<span class="ruby-identifier">name</span>)
      <span class="ruby-ivar">@attributes_cache</span>[<span class="ruby-identifier">name</span>] = <span class="ruby-identifier">column</span>.<span class="ruby-identifier">type_cast</span>(<span class="ruby-identifier">value</span>)
    <span class="ruby-keyword">else</span>
      <span class="ruby-identifier">column</span>.<span class="ruby-identifier">type_cast</span> <span class="ruby-identifier">value</span>
    <span class="ruby-keyword">end</span>
  }
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
                    </div>

    </div>
  </body>
</html>    